<cfinclude template="/ecada/header.cfm">

<cfquery datasource="exp" name="experiments">
    	select * from givesanswer where trialid=#trialid# order by participantid
    </cfquery>
    
    
<cfquery datasource="exp" name="experimentscorrect">
    	select participantid, stimuli.answer, hour, minute, second, hour2, minute2, second2 
    	from givesanswer,trial, stimuli 
    	where trialid=#trialid# 
    	and givesanswer.trialid=trial.id
    	and stimuli.id=trial.stimid
    	and trial.id=#trialid#
    	order by participantid

    </cfquery>

<cfquery datasource="exp" name="timing">
    	select participantid, hour, minute, second, hour2, minute2, second2 
    	from givesanswer,trial, stimuli 
    	where trialid=#trialid# 
    	and givesanswer.trialid=trial.id
    	and stimuli.id=trial.stimid
    	and trial.id=#trialid#
    	order by participantid
    </cfquery>
    
<cfset times = ArrayNew(1)>
<cfset partid = ArrayNew(1)>
<cfloop query = "timing">

<cfset MyTime="#hour#:#minute#:#second#">  
<cfset MyAnotherTime="#hour2#:#minute2#:#second2#">    
<cfset MyDateTime=CreateDateTime(Year(Now()),Month(Now()),Day(Now()),#hour#,#minute#,#second#)>  
<cfset MyDateTime2=CreateDateTime(Year(Now()),Month(Now()),Day(Now()),#hour2#,#minute2#,#second2#)>  
<cfset ArrayAppend(times, DateDiff("s", "#MyDateTime#", "#MyDateTime2#"))>
<cfset ArrayAppend(partid, "#participantid#")>

</cfloop>
    
<cfquery datasource="exp" name="trialinfo">
    	select question, answer, location from trial, stimuli where trial.id=#trialid#
    	and trial.stimid=stimuli.id
    </cfquery>
    
<cfquery datasource="exp" name="getmax">
select max(answer) as maxnum from givesanswer where trialid=#trialid#
</cfquery>

<cfquery datasource="exp" name="getmin">
select min(answer) as minnum from givesanswer where trialid=#trialid#
</cfquery>


<!---START OF DETAIL--->
<cfquery datasource="exp" name="getall">
select answer from givesanswer where trialid=#trialid#
</cfquery>

<cfset ans = ArrayNew(1)>
<cfloop query="getall">
  <cfset ArrayAppend(ans, "#answer#")>
</cfloop>
<cfset total = 0>
<cfset number = #ArrayLen(ans)#>
<cfloop index = "i" from = "1" to = "#ArrayLen(ans)#">
<cfset total = total + ans[i]>
</cfloop>
<!--- this is to calculate the average, max and min--->
<cfset average = total/number>
<cfset average = #round(average*100)#/100>
<cfset max = #ArrayMax(ans)#>
<cfset min = #ArrayMin(ans)#> 

<cfset Temp=ArraySort(ans,"Numeric","desc")>  
  
<!--- this is to calculate the std deviation--->
<cfset s = 0>
<cfloop from="1" to="#ArrayLen(times)#" index="i">
  <cfset d = times[i]-#ArrayAvg(times)#>
  <cfset s = s + (d*d)>
</cfloop>
<cfset st = s/#ArrayLen(times)#>
<cfset sd = round(sqr(st))>
<!--- this is to calculate the median--->
<cfif #ArrayLen(times)# mod 2 is 0>
<cfset firstmed = times[(#ArrayLen(times)#/2)+1]>
<cfset secondmed = times[(#ArrayLen(times)#/2)-1]>
<cfset median = (firstmed+secondmed)/2>
<cfelse>
<cfset median = times[#ArrayLen(times)#/2]>
</cfif>
<!---END OF DETAIL--->

<cfset startval=ArrayMin(times)>
<cfset startval=0>
<cfset endval = ArrayMax(times)*1.25>


<cfoutput>
<h1>View timing by trial</h1> <br>
<table><tr><td>
<img src="/ecada/#trialinfo.location#" width=150></td>
<td>Question:  #trialinfo.question#<br>
Answer: #trialinfo.answer#<br>
Average time (seconds): #round(ArrayAvg(times)*100)/100# <br>
median #median# <br>
standard deviation #sd# <br>
max #ArrayMax(times)# <br>
min #ArrayMin(times)# <br>
</td></tr></table>

<cfchart format="flash" title="Time per participant (seconds)" chartheight="400" chartwidth="500" scalefrom="#startval#" scaleto="#endval#" seriesplacement="default" showborder="yes" labelformat="number" xaxistitle="Participant Id" yaxistitle="Timing (seconds)" tipstyle="mouseOver">
<cfchartseries type="line" serieslabel="Time taken per participant (Line)" seriescolor="FF00FF" markerstyle="circle" >
<cfloop index="i" from="1" to="#arrayLen(times)#">   <cfchartdata item="#partid[i]#" value="#times[i]#" /> </cfloop> 
</cfchartseries>  
 
<cfchartseries type="line" itemcolumn="participantid" valuecolumn="seconds" serieslabel="Average Time" seriescolor="00ff00">
<cfloop index="i" from="1" to="#arrayLen(times)#">   <cfchartdata item=#partid[i]# value="#ArrayAvg(times)#" /> </cfloop> 
</cfchartseries>

</cfchart>
<cfchart
   format="flash"
   title="Time per participant (seconds)"
   showBorder="yes"
   chartheight="400" chartwidth="500"
   scalefrom="#startval#"
   scaleto="#endval#"
   xaxistitle="Participant Id" yaxistitle="Timing (seconds)">
  <cfchartseries
      type="bar"
      serieslabel="Time taken per participant (Bar)"
      seriescolor="blue">
  <cfloop index="i" from="1" to="#arrayLen(times)#">   <cfchartdata item="#partid[i]#" value="#times[i]#" /> </cfloop>  
  </cfchartseries>
  
  <cfchartseries
      type="line"
      serieslabel="Average time"
      seriescolor="00ff00">
  <cfloop index="i" from="1" to="#arrayLen(times)#">   <cfchartdata item=#partid[i]# value="#ArrayAvg(times)#" /> </cfloop> 
    
  </cfchartseries>
</cfchart>

 </cfoutput>

<cfinclude template="/ecada/footer.cfm">
